<?php

class helper {
	static public function head_register() {
		?><style type="text/css"><?php
			$opt_name = 'tema_casamenteiras_padrao';
			$opt_val = get_option( $opt_name );
			$opt_val = array(
				'texto_padrao' => ( $opt_val['cor_texto_padrao'] ) ? $opt_val['cor_texto_padrao'] : "444444",
				'link_padrao' => ( $opt_val['link_padrao'] ) ? $opt_val['link_padrao'] : "F7BBD5",
				'link_titulo_post' => ( $opt_val['link_titulo_post'] ) ? $opt_val['link_titulo_post'] : "263A6D",
				'texto_titulo_lateral' => ( $opt_val['texto_titulo_lateral'] ) ? $opt_val['texto_titulo_lateral'] : "263A6D",
				'link_lateral' => ( $opt_val['link_lateral'] ) ? $opt_val['link_lateral'] : "263A6D",
				'css' => ( $opt_val['css'] ) ? $opt_val['css'] : "",
			);
?>
			body, input, textarea, #content { color: #<?php echo $opt_val['texto_padrao'] ?>; }
			a, a:link, a:hover { color: #<?php echo $opt_val['link_padrao'] ?>; }
			a:hover { color: #<?php echo $opt_val['link_padrao'] ?> !important; }
			ul.dots li a, ul.dots li a:hover,
			ul#featured li blockquote p,
			ul#featured li blockquote h4 a, ul#featured li blockquote h4 a:hover,
			.postContainer .posts ul li h3 a, .postContainer .posts ul li h3 a:hover,
			.comment-author cite a, #breadcrumb a, .entry-title a:link,
			.entry-title a:visited, .category-links a { color: #<?php echo $opt_val['link_titulo_post'] ?> !important; }
			.widget-title { color: #<?php echo $opt_val['texto_titulo_lateral'] ?>; }
			.widget-area a { color: #<?php echo $opt_val['link_lateral'] ?>; }
			<?php echo $opt_val['css'] ?>
		</style><?php
	}

	static public function plugin_menu() {
		add_theme_page('Opções de Fontes', 'Fontes', 'administrator', 'theme_config', array('helper', 'plugin_options'));
		add_theme_page('Opções de Imagens', 'Imagens', 'administrator', 'theme_images', array('helper', 'plugin_images'));
		add_theme_page('Opções da Home', 'Posts da Home', 'administrator', 'theme_home_posts', array('helper', 'theme_home_posts'));
		add_theme_page('Opções de Categorias', 'Categorias', 'administrator', 'plugin_category_options', array('helper', 'plugin_category_options'));
	}
	
	static public function plugin_category_options() {		

		$opt_name = 'tema_casamenteiras_padrao_categoria';
		$opt_val = get_option( $opt_name );

		if (isset($_POST)) {
			if ( ! $opt_val) {
				$opt_val = array();
			};
			foreach(self::get_listCats() as $cat) {
				if (isset($_POST['color-'.$cat->cat_ID])) {
					$opt_val['color-'.$cat->cat_ID] = $_POST['color-'.$cat->cat_ID];
				};
			};
			$i = 0;
			
			$keys = array_keys($_FILES);
			foreach ($_FILES as $image) {
				// if a files was upload
				if ($image['size']) {
					// if it is an image
					if (preg_match('/(jpg|jpeg|png|gif)$/', $image['type'])) {
						$override = array('test_form' => false);
						$file = wp_handle_upload($image, $override);
						$opt_val[$keys[$i]] = $file['url'];
					} else {
						$options = get_option('plugin_category_options');
						$opt_val[$keys[$i]] = $options[$logo];
						$current_key = $keys[$i];
						wp_die('Nenhuma imagem enviada.');
					};
				} else {
					$options = get_option('plugin_category_options');
					$opt_val[$keys[$i]] = $options[$keys[$i]];
				}
				$i++;
			}
			update_option( $opt_name, $opt_val );
			echo '<div class="updated"><p><strong>Opções salvas com sucesso.</strong></p></div>';
		}

		if ( isset($_GET['reset']) ) {
			unset($opt_val[$_GET['reset']]);
			echo '<div class="updated"><p><strong>A imagem foi removida com sucesso.</strong></p></div>';
			update_option( $opt_name, $opt_val );
		};

		echo '<div class="wrap">';
		echo "<h2>Opções de categorias:</h2>";
		
		$uri = explode('plugin_category_options', $_SERVER['REQUEST_URI']);
		$form_url = 'http://'.$_SERVER['SERVER_NAME'].$uri[0].'plugin_category_options';
		unset($uri);

?>
				<form name="layout_personalize_<?php echo $cat->cat_ID ?>" id="layout_personalize_<?php echo $cat->cat_ID ?>" method="post" action="<?php echo $form_url ?>" enctype="multipart/form-data">
					
					<input type="hidden" name="hidden" value="Y">

					<h3>Padrão</h3>
					<div id="personalizarCSS">
						<p>
							<label>Imagem do topo</label>
							<input type="file" id="image-default" name="image-default" /><br />
							<?php if (isset($opt_val['image-default'])) : ?>
								<img src="<?php echo $opt_val['image-default'] ?>" /> <?php echo self::resetOption('image-default', 'plugin_category_options', 'remover') ?>
							<?php endif; ?>
						</p>
					</div>
					<p class="submit">
						<input type="submit" name="Submit" value="Salvar" />
					</p>
				</form><br /><br /><br />
				<hr />
<?php
			foreach(self::get_listCats() as $cat) :
?>
				<form name="layout_personalize_<?php echo $cat->cat_ID ?>" id="layout_personalize_<?php echo $cat->cat_ID ?>" method="post" action="<?php echo $form_url ?>" enctype="multipart/form-data">
					
					<input type="hidden" name="hidden" value="Y">
					

					<h3><?php echo $cat->cat_name ?></h3>
					<div id="personalizarCSS">
						<p class="cores">
							<label>Cor de fundo</label>
							<input type="text" id="color-<?php echo $cat->cat_ID ?>" class="colorpickerField" name="color-<?php echo $cat->cat_ID ?>" value="<?php echo ((isset($opt_val['color-'.$cat->cat_ID])) ? $opt_val['color-'.$cat->cat_ID] : ''); ?>" size="6"><span style="background:#<?php echo ((isset($opt_val['color-'.$cat->cat_ID])) ? $opt_val['color-'.$cat->cat_ID] : ''); ?>"></span>
						</p>
						<p>
							<label>Imagem do topo</label>
							<input type="file" id="image-<?php echo $cat->cat_ID ?>" name="image-<?php echo $cat->cat_ID ?>" /><br />
							<?php if (isset($opt_val['image-'.$cat->cat_ID])) : ?>
							<img src="<?php echo $opt_val['image-'.$cat->cat_ID]; ?>" /> <?php echo self::resetOption('image-'.$cat->cat_ID, 'plugin_category_options', 'remover') ?>
							<?php endif; ?>
						</p>
					</div>
					<p class="submit">
						<input type="submit" name="Submit" value="Salvar" />
					</p>
				</form>
				<hr />
<?php
			endforeach;
?>
		</div>
		
		<style>#css { height:200px; margin-bottom:40px; width:75%; }</style>
		<script type="text/javascript">
			window.location.href = '#'<?php echo $current_key ?>;
			if ( ! window.jQuery ) document.write('<scr'+'ipt type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></scr'+'ipt>')
		</script>
		<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/colorpicker/js/colorpicker.js"></script>
		<link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/colorpicker/css/colorpicker.css" type="text/css" />
	
		<script type="text/javascript">
			( function($) {
				$(window).ready( function() {
					
					// Color picker
					$('input.colorpickerField').ColorPicker({
						onSubmit: function(hsb, hex, rgb, el) {
							$(el).val(hex);
							$(el).ColorPickerHide();
							$(el).parent().find('span').css({ background: '#'+hex });
						},
						onBeforeShow: function () {
							$(this).ColorPickerSetColor(this.value);
						}
					})
					.bind('keyup', function(){
						$(this).ColorPickerSetColor(this.value);
					});
					
					$('#layout_personalize').submit( function() {
						$('#layout_personalize').find('input[type="file"]').each( function() {
							var $that = $(this);
							if ($that.val() === '')
								return $that.remove();
						});
					});

				});
			})(jQuery);
		</script>
		<style>
			.cores span {
				-moz-border-radius:2px 2px 2px 2px;
				background:none repeat scroll 0 0 #FFFFFF;
				border:1px solid #CCCCCC;
				display:inline;
				float:left;
				height:19px;
				margin:2px 3px 0;
				text-indent:-999px;
				width:19px;
			}
		</style>
		
		<?php
	}
	
	static public function plugin_options() {		
		
		$opt_name = 'tema_casamenteiras_padrao';
		$hidden_field_name = 'hidden';
		$opt_val = get_option( $opt_name );
		$opt_val = array(
			'texto_padrao' => ( isset($_POST['texto_padrao']) ) ? $_POST['texto_padrao'] : (isset($opt_val['texto_padrao'])?$opt_val['texto_padrao']:'444444'),
			'link_padrao' => ( isset($_POST['link_padrao']) ) ? $_POST['link_padrao'] : (isset($opt_val['link_padrao'])?$opt_val['link_padrao']:'F7BBD5'),
			'link_titulo_post' => ( isset($_POST['link_titulo_post']) ) ? $_POST['link_titulo_post'] : (isset($opt_val['link_titulo_post'])?$opt_val['link_titulo_post']:'263A6D'),
			'texto_titulo_lateral' => ( isset($_POST['texto_titulo_lateral']) ) ? $_POST['texto_titulo_lateral'] : (isset($opt_val['texto_titulo_lateral'])?$opt_val['texto_titulo_lateral']:'263A6D'),
			'link_lateral' => ( isset($_POST['link_lateral']) ) ? $_POST['link_lateral'] : (isset($opt_val['link_lateral'])?$opt_val['link_lateral']:'263A6D'),
			'css' => ( isset($_POST['css']) ) ? $_POST['css'] : (isset($opt_val['css'])?$opt_val['css']:''),
		);
		if( $_POST[ $hidden_field_name ] == 'Y' ) {
			update_option( $opt_name, $opt_val );
			echo '<div class="updated"><p><strong>Opções salvas com sucesso.</strong></p></div>';
		}
		
		echo '<div class="wrap">';
		echo "<h2>Opções de fontes:</h2>";
		?>
			<form name="layout_personalize" id="layout_personalize" method="post" action="">
				
				<input type="hidden" name="hidden" value="Y">
				<h3>Cores</h3>
			
				<div id="personalizarCSS">
				
					<h4>Texto</h4>
					<p class="cores">
						<label><?php _e("Texto Padrão:", 'domain' ); ?></label>
						<input type="text" id="texto_padrao" class="colorpickerField" name="texto_padrao" value="<?php echo $opt_val['texto_padrao']; ?>" size="6"><span style="background:#<?php echo $opt_val['texto_padrao']; ?>"></span>
					</p>
					<p class="cores">
						<label><?php _e("Link padrão:", 'domain' ); ?></label>
						<input type="text" id="link_padrao" class="colorpickerField" name="link_padrao" value="<?php echo $opt_val['link_padrao']; ?>" size="6"><span style="background:#<?php echo $opt_val['link_padrao']; ?>"></span>
					</p>
					<p class="cores">
						<label><?php _e("Link Título Post:", 'domain' ); ?></label>
						<input type="text" id="link_titulo_post" class="colorpickerField" name="link_titulo_post" value="<?php echo $opt_val['link_titulo_post']; ?>" size="6"><span style="background:#<?php echo $opt_val['link_titulo_post']; ?>"></span>
					</p>
					<p class="cores">
						<label><?php _e("Texto título lateral:", 'domain' ); ?></label>
						<input type="text" id="texto_titulo_lateral" class="colorpickerField" name="texto_padrao" value="<?php echo $opt_val['texto_titulo_lateral']; ?>" size="6"><span style="background:#<?php echo $opt_val['texto_titulo_lateral']; ?>"></span>
					</p>
					<p class="cores">
						<label><?php _e("Link lateral:", 'domain' ); ?></label>
						<input type="text" id="link_lateral" class="colorpickerField" name="link_lateral" value="<?php echo $opt_val['link_lateral']; ?>" size="6"><span style="background:#<?php echo $opt_val['link_lateral']; ?>"></span>
					</p>
					<hr />
					
					<h4><?php _e("CSS Personalizado:", 'domain' ); ?></h4>
					<p id="css">
						<textarea style="display:block;" id="css" name="css"><?php echo $opt_val['css']; ?></textarea>
					</p>
				</div>
				
				<p class="submit">
					<input type="submit" name="Submit" value="<?php _e('Atualizar', 'domain' ) ?>" />
				</p>
			</form>
		</div>
		
		<style>#css { height:200px; margin-bottom:40px; width:75%; }</style>
		<script type="text/javascript">
			if ( ! window.jQuery ) document.write('<scr'+'ipt type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></scr'+'ipt>')
		</script>
		<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/colorpicker/js/colorpicker.js"></script>
		<link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/colorpicker/css/colorpicker.css" type="text/css" />
	
		<script type="text/javascript">
			( function($) {
				$(window).ready( function() {
					
					// Color picker
					$('input.colorpickerField').ColorPicker({
						onSubmit: function(hsb, hex, rgb, el) {
							$(el).val(hex);
							$(el).ColorPickerHide();
							$(el).parent().find('span').css({ background: '#'+hex });
						},
						onBeforeShow: function () {
							$(this).ColorPickerSetColor(this.value);
						}
					})
					.bind('keyup', function(){
						$(this).ColorPickerSetColor(this.value);
					});

				});
			})(jQuery);
		</script>
		<style>
			.cores span {
				-moz-border-radius:2px 2px 2px 2px;
				background:none repeat scroll 0 0 #FFFFFF;
				border:1px solid #CCCCCC;
				display:inline;
				float:left;
				height:19px;
				margin:2px 3px 0;
				text-indent:-999px;
				width:19px;
			}
		</style>
		
		<?php
	}
	
	static public function get_option($options = 'tema_casamenteiras_imagens', $option = 'logo', $default = FALSE)
	{
		$options = get_option($options);
		return ( $options[$option] ) ? $options[$option] : $default;
	}
	
	static public function plugin_images() {		
		$opt_name = 'tema_casamenteiras_imagens';
		$opt_val = get_option( $opt_name );

		if ( ! empty($_POST) ) {
		
			$opt_val = array(
				'logo' => (isset($opt_val['logo'])) ? $opt_val['logo'] : get_bloginfo('template_url').'/images/logo.png',
				'logo-internal' => (isset($opt_val['logo-internal'])) ? $opt_val['logo-internal'] : '',
			
				'rss-title' => (isset($_POST['rss-title'])) ? $_POST['rss-title'] : 'RSS',
				'rss-url' => (isset($_POST['rss-url'])) ? $_POST['rss-url'] : 'http://feeds.feedburner.com/casamenteiras',
				'rss-image' => (isset($_POST['rss-image'])) ? $_POST['rss-image'] : get_bloginfo('template_url').'/images/social-rss.png',
				
				'twitter-title' => (isset($_POST['twitter-title'])) ? $_POST['twitter-title'] : 'Twitter',
				'twitter-url' => (isset($_POST['twitter-url'])) ? $_POST['twitter-url'] : 'http://twitter.com/casamenteiras',
				'twitter-image' => (isset($_POST['twitter-image'])) ? $_POST['twitter-image'] : get_bloginfo('template_url').'/images/social-twitter.png',
				
				'facebook-title' => (isset($_POST['facebook-title'])) ? $_POST['facebook-title'] : 'Facebook',
				'facebook-url' => (isset($_POST['facebook-url'])) ? $_POST['facebook-url'] : 'http://www.facebook.com/pages/Casamenteiras/110401952361055',
				'facebook-image' => (isset($_POST['facebook-image'])) ? $_POST['facebook-image'] : get_bloginfo('template_url').'/images/social-facebook.png',
				
				'clube-title' => (isset($_POST['clube-title'])) ? $_POST['clube-title'] : 'Clube',
				'clube-url' => (isset($_POST['clube-url'])) ? $_POST['clube-url'] : 'http://clube.casamenteiras.com.br/',
				'clube-image' => (isset($_POST['clube-image'])) ? $_POST['clube-image'] : get_bloginfo('template_url').'/images/social-clube.png',
				
				'youtube-title' => (isset($_POST['youtube-title'])) ? $_POST['youtube-title'] : 'You Tube',
				'youtube-url' => (isset($_POST['youtube-url'])) ? $_POST['youtube-url'] : 'http://www.youtube.com/casamenteiras',
				'youtube-image' => (isset($_POST['youtube-image'])) ? $_POST['youtube-image'] : get_bloginfo('template_url').'/images/social-youtube.png',
			);
			$keys = array_keys($_FILES);
			$i = 0;

			foreach ($_FILES as $image) {
			  // if a files was upload
			  if ($image['size']) {
				 // if it is an image
				 if (preg_match('/(jpg|jpeg|png|gif)$/', $image['type'])) {
					$override = array('test_form' => false);
					$file = wp_handle_upload($image, $override);

					$opt_val[$keys[$i]] = $file['url'];
				 } else {
					/*
					$options = get_option('plugin_images');
					$opt_val[$keys[$i]] = $options[$logo];
					wp_die('Nenhuma imagem enviada.');
					*/
				 }
			  }
			  $i++;
			}
			update_option( $opt_name, $opt_val );
			echo '<div class="updated"><p><strong>Opções salvas com sucesso.</strong></p></div>';
		}

		if ( isset($_GET['reset']) ) {
			unset($opt_val[$_GET['reset']]);
			echo '<div class="updated"><p><strong>A imagem foi restaurada com sucesso.</strong></p></div>';
			update_option( $opt_name, $opt_val );
		};
		
		$opt_val = array(
			'logo' => (isset($opt_val['logo'])) ? $opt_val['logo'] : get_bloginfo('template_url').'/images/logo.png',
			'logo-internal' => (isset($opt_val['logo-internal'])) ? $opt_val['logo-internal'] : '',
			
			'rss-title' => (isset($opt_val['rss-title'])) ? $opt_val['rss-title'] : 'RSS',
			'rss-url' => (isset($opt_val['rss-url'])) ? $opt_val['rss-url'] : 'http://feeds.feedburner.com/casamenteiras',
			'rss-image' => (isset($opt_val['rss-image'])) ? $opt_val['rss-image'] : get_bloginfo('template_url').'/images/social-rss.png',
			
			'twitter-title' => (isset($opt_val['twitter-title'])) ? $opt_val['twitter-title'] : 'Twitter',
			'twitter-url' => (isset($opt_val['twitter-url'])) ? $opt_val['twitter-url'] : 'http://twitter.com/casamenteiras',
			'twitter-image' => (isset($opt_val['twitter-image'])) ? $opt_val['twitter-image'] : get_bloginfo('template_url').'/images/social-twitter.png',
			
			'facebook-title' => (isset($opt_val['facebook-title'])) ? $opt_val['facebook-title'] : 'Facebook',
			'facebook-url' => (isset($opt_val['facebook-url'])) ? $opt_val['facebook-url'] : 'http://www.facebook.com/pages/Casamenteiras/110401952361055',
			'facebook-image' => (isset($opt_val['facebook-image'])) ? $opt_val['facebook-image'] : get_bloginfo('template_url').'/images/social-facebook.png',
			
			'clube-title' => (isset($opt_val['clube-title'])) ? $opt_val['clube-title'] : 'Clube',
			'clube-url' => (isset($opt_val['clube-url'])) ? $opt_val['clube-url'] : 'http://clube.casamenteiras.com.br/',
			'clube-image' => (isset($opt_val['clube-image'])) ? $opt_val['clube-image'] : get_bloginfo('template_url').'/images/social-clube.png',
			
			'youtube-title' => (isset($opt_val['youtube-title'])) ? $opt_val['youtube-title'] : 'You Tube',
			'youtube-url' => (isset($opt_val['youtube-url'])) ? $opt_val['youtube-url'] : 'http://www.youtube.com/casamenteiras',
			'youtube-image' => (isset($opt_val['youtube-image'])) ? $opt_val['youtube-image'] : get_bloginfo('template_url').'/images/social-youtube.png',
		);
		
		echo '<div class="wrap">';
		echo "<h2>Imagens:</h2>";
		
		$uri = explode('theme_images', $_SERVER['REQUEST_URI']);
		$form_url = 'http://'.$_SERVER['SERVER_NAME'].$uri[0].'theme_images';
		unset($uri);
		?>
			
			<form name="layout_personalize" id="layout_personalize" method="post" action="<?php echo $form_url ?>" enctype="multipart/form-data">

				<h3>Imagens</h3>
				<div id="">
					<h4>Logo:</h4>
					<p class="">
						<input type="file" id="logo" name="logo" />
					</p>
					<p>
						<img src="<?php echo $opt_val['logo']; ?>" /> <?php echo self::resetOption('logo') ?>
					</p>
					
					<h4>Logo internas:</h4>
					<p class="">
						<input type="file" id="logo-internal" name="logo-internal" />
					</p>
					<p>
						<img src="<?php echo $opt_val['logo-internal']; ?>" /> <?php echo self::resetOption('logo-internal') ?>
					</p>
				</div>
				<hr />
				
				<h3>Menu Social</h3>
				<div id="">
					<h4>RSS</h4>
					<p class="">
						<label>Título:</label>
						<input type="text" id="rss-title" name="rss-title" value="<?php echo $opt_val['rss-title'] ?>" size="100" />
						<br />
						<label>Link:</label>
						<input type="text" id="rss-url" name="rss-url" value="<?php echo $opt_val['rss-url'] ?>" size="100" />
						<br />
						<label>Imagem:</label>
						<input type="file" id="rss-image" name="rss-image" />
					</p>
					<p>
						<img src="<?php echo $opt_val['rss-image']; ?>" /> <?php echo self::resetOption('rss-image') ?>
					</p>
					<hr >
					
					<h4>Twitter</h4>
					<p class="">
						<label>Título:</label>
						<input type="text" id="twitter-title" name="twitter-title" value="<?php echo $opt_val['twitter-title'] ?>" size="100" />
						<br />
						<label>Link:</label>
						<input type="text" id="twitter-url" name="twitter-url" value="<?php echo $opt_val['twitter-url'] ?>" size="100" />
						<br />
						<label>Imagem:</label>
						<input type="file" id="twitter-image" name="twitter-image" />
					</p>
					<p>
						<img src="<?php echo $opt_val['twitter-image']; ?>" /> <?php echo self::resetOption('twitter-image') ?>
					</p>
					<hr >
					
					<h4>Facebook</h4>
					<p class="">
						<label>Título:</label>
						<input type="text" id="facebook-title" name="facebook-title" value="<?php echo $opt_val['facebook-title'] ?>" size="100" />
						<br />
						<label>Link:</label>
						<input type="text" id="facebook-url" name="facebook-url" value="<?php echo $opt_val['facebook-url'] ?>" size="100" />
						<br />
						<label>Imagem:</label>
						<input type="file" id="facebook-image" name="facebook-image" />
					</p>
					<p>
						<img src="<?php echo $opt_val['facebook-image']; ?>" /> <?php echo self::resetOption('facebook-image') ?>
					</p>
					<hr >
					
					<h4>Clube</h4>
					<p class="">
						<label>Título:</label>
						<input type="text" id="clube-title" name="clube-title" value="<?php echo $opt_val['clube-title'] ?>" size="100" />
						<br />
						<label>Link:</label>
						<input type="text" id="clube-url" name="clube-url" value="<?php echo $opt_val['clube-url'] ?>" size="100" />
						<br />
						<label>Imagem:</label>
						<input type="file" id="clube-image" name="clube-image" />
					</p>
					<p>
						<img src="<?php echo $opt_val['clube-image']; ?>" /> <?php echo self::resetOption('clube-image') ?>
					</p>
					<hr >
					
					<h4>Youtube</h4>
					<p class="">
						<label>Título:</label>
						<input type="text" id="youtube-title" name="youtube-title" value="<?php echo $opt_val['youtube-title'] ?>" size="100" />
						<br />
						<label>Link:</label>
						<input type="text" id="youtube-url" name="youtube-url" value="<?php echo $opt_val['youtube-url'] ?>" size="100" />
						<br />
						<label>Imagem:</label>
						<input type="file" id="youtube-image" name="youtube-image" />
					</p>
					<p>
						<img src="<?php echo $opt_val['youtube-image']; ?>" /> <?php echo self::resetOption('youtube-image') ?>
					</p>
					<hr >
					
				</div>
				
				<p class="submit">
					<input type="submit" name="Submit" value="Salvar" />
				</p>
			</form>
		</div>
		<?php
	 
	}
	
	private static function resetOption($reset = '', $theme = 'theme_images', $text = 'restaurar')
	{
		$uri = explode($theme, $_SERVER['REQUEST_URI']);
		return '<a href="http://'.$_SERVER['SERVER_NAME'].$uri[0].$theme.'&reset='.$reset.'">'.$text.'</a>';
	}
	
	public static function social_nav() {
		$opt_val = get_option('tema_casamenteiras_imagens');
		$opt_val = array(
			'rss-title'	=> (isset($opt_val['rss-title'])) ? $opt_val['rss-title'] : 'RSS',
			'rss-url'	=> (isset($opt_val['rss-url'])) ? $opt_val['rss-url'] : 'http://feeds.feedburner.com/casamenteiras',
			'rss-image'	=> (isset($opt_val['rss-image'])) ? $opt_val['rss-image'] : get_bloginfo('template_url').'/images/social-rss.png',
			
			'twitter-title'	=> (isset($opt_val['twitter-title'])) ? $opt_val['twitter-title'] : 'Twitter',
			'twitter-url'	=> (isset($opt_val['twitter-url'])) ? $opt_val['twitter-url'] : 'http://twitter.com/casamenteiras',
			'twitter-image'	=> (isset($opt_val['twitter-image'])) ? $opt_val['twitter-image'] : get_bloginfo('template_url').'/images/social-twitter.png',
			
			'facebook-title'	=> (isset($opt_val['facebook-title'])) ? $opt_val['facebook-title'] : 'Facebook',
			'facebook-url'		=> (isset($opt_val['facebook-url'])) ? $opt_val['facebook-url'] : 'http://www.facebook.com/pages/Casamenteiras/110401952361055',
			'facebook-image'	=> (isset($opt_val['facebook-image'])) ? $opt_val['facebook-image'] : get_bloginfo('template_url').'/images/social-facebook.png',
			
			'clube-title'	=> (isset($opt_val['clube-title'])) ? $opt_val['clube-title'] : 'Clube',
			'clube-url'		=> (isset($opt_val['clube-url'])) ? $opt_val['clube-url'] : 'http://clube.casamenteiras.com.br/',
			'clube-image'	=> (isset($opt_val['clube-image'])) ? $opt_val['clube-image'] : get_bloginfo('template_url').'/images/social-clube.png',
			
			'youtube-title'	=> (isset($opt_val['youtube-title'])) ? $opt_val['youtube-title'] : 'You Tube',
			'youtube-url'	=> (isset($opt_val['youtube-url'])) ? $opt_val['youtube-url'] : 'http://www.youtube.com/casamenteiras',
			'youtube-image'	=> (isset($opt_val['youtube-image'])) ? $opt_val['youtube-image'] : get_bloginfo('template_url').'/images/social-youtube.png',
		);
		return '<ul id="social-nav">'.
			'<li class="rss"><a target="_blank" href="'.$opt_val['rss-url'].'" style="background-image:url(\''.$opt_val['rss-image'].'\')">'.$opt_val['rss-title'].'</a></li>'.
			'<li class="twitter"><a target="_blank" href="'.$opt_val['twitter-url'].'" style="background-image:url(\''.$opt_val['twitter-image'].'\')">'.$opt_val['twitter-title'].'</a></li>'.
			'<li class="facebook"><a target="_blank" href="'.$opt_val['facebook-url'].'" style="background-image:url(\''.$opt_val['facebook-image'].'\')">'.$opt_val['facebook-title'].'</a></li>'.
			'<li class="clube"><a target="_blank" href="'.$opt_val['clube-url'].'" style="background-image:url(\''.$opt_val['clube-image'].'\')">'.$opt_val['clube-title'].'</a></li>'.
			'<li class="youtube"><a target="_blank" href="'.$opt_val['youtube-url'].'" style="background-image:url(\''.$opt_val['youtube-image'].'\')">'.$opt_val['youtube-title'].'</a></li>'.
		'</ul>';
	}
	
	private static function get_listCats() {
		return get_categories('hide_empty=0');
	}
	
	static public function listCats($id = NULL, $selected = NULL) {
		$listCats = ($id===NULL)?'<select>':'<select id="'.$id.'" name="'.$id.'">';
		;
		foreach (self::get_listCats() as $cat) {
			$listCats .= '<option value="'.$cat->cat_ID.'"'.(($selected==$cat->cat_ID || $selected==$cat->cat_name)?' selected="selected"':'').'>'.$cat->cat_name.'</option>';
		}
		return $listCats.'</select>';
	}
	
	static public function theme_home_posts() {		
		$opt_name = 'tema_casamenteiras_home_posts';
		$opt_val = get_option($opt_name);
		
		if ( ! empty($_POST) ) {

			$opt_val = array(
				'highlight-actived'		=> ($_POST['highlight-actived'] === NULL) ? 0 : 1,
				'highlight-category'	=> (isset($_POST['highlight-category'])) ? $_POST['highlight-category'] : ((isset($opt_val['highlight-category'])) ? $opt_val['highlight-category'] : 1),
				'active-categories'		=> (isset($_POST['active-categories'])) ? $_POST['active-categories'] : ((isset($opt_val['active-categories'])) ? $opt_val['highlight-actived'] : 0),
				
				'1-category-id'	=> (isset($_POST['1-category-id'])) ? $_POST['1-category-id'] : ((isset($opt_val['1-category-id'])) ? $opt_val['1-category-id'] : 1),
				'1-title'		=> (isset($_POST['1-title'])) ? $_POST['1-title'] : ((isset($opt_val['1-title'])) ? $opt_val['1-title'] : ''),
				'1-image'		=> (isset($_POST['1-image'])) ? $_POST['1-image'] : ((isset($opt_val['1-image'])) ? $opt_val['1-image'] : get_bloginfo('template_url').'/images/categoria-Casamento.png'),
				'1-posts'		=> (isset($_POST['1-posts'])) ? $_POST['1-posts'] : ((isset($opt_val['1-posts'])) ? $opt_val['1-posts'] : 3),
				
				'2-category-id'	=> (isset($_POST['2-category-id'])) ? $_POST['2-category-id'] : ((isset($opt_val['2-category-id'])) ? $opt_val['2-category-id'] : 1),
				'2-title'		=> (isset($_POST['2-title'])) ? $_POST['2-title'] : ((isset($opt_val['2-title'])) ? $opt_val['2-title'] : ''),
				'2-image'		=> (isset($_POST['2-image'])) ? $_POST['2-image'] : ((isset($opt_val['2-image'])) ? $opt_val['2-image'] : get_bloginfo('template_url').'/images/categoria-Casamento.png'),
				'2-posts'		=> (isset($_POST['2-posts'])) ? $_POST['2-posts'] : ((isset($opt_val['2-posts'])) ? $opt_val['2-posts'] : 3),
				
				'3-category-id'	=> (isset($_POST['3-category-id'])) ? $_POST['3-category-id'] : ((isset($opt_val['3-category-id'])) ? $opt_val['3-category-id'] : 1),
				'3-title'		=> (isset($_POST['3-title'])) ? $_POST['3-title'] : ((isset($opt_val['3-title'])) ? $opt_val['3-title'] : ''),
				'3-image'		=> (isset($_POST['3-image'])) ? $_POST['3-image'] : ((isset($opt_val['3-image'])) ? $opt_val['3-image'] : get_bloginfo('template_url').'/images/categoria-Casamento.png'),
				'3-posts'		=> (isset($_POST['3-posts'])) ? $_POST['3-posts'] : ((isset($opt_val['3-posts'])) ? $opt_val['3-posts'] : 3),
			);
			
			$keys = array_keys($_FILES);
			$i = 0;

			foreach ($_FILES as $image) {
			  // if a files was upload
			  if ($image['size']) {
				 // if it is an image
				 if (preg_match('/(jpg|jpeg|png|gif)$/', $image['type'])) {
					$override = array('test_form' => false);
					$file = wp_handle_upload($image, $override);

					$opt_val[$keys[$i]] = $file['url'];
				 } else {
					wp_die('Nenhuma imagem enviada.');
				 }
			  }

			  // else, retain the image that's already on file.
			  else {
				 // $options = get_option('plugin_home_images');
				 // $opt_val[$keys[$i]] = $options[$keys[$i]];
			  }
			  $i++;
			}
			update_option( $opt_name, $opt_val );
			echo '<div class="updated"><p><strong>Opções salvas com sucesso.</strong></p></div>';
		}

		if ( isset($_GET['reset']) ) {
			unset($opt_val[$_GET['reset']]);
			echo '<div class="updated"><p><strong>A imagem foi restaurada com sucesso.</strong></p></div>';
			update_option( $opt_name, $opt_val );
		};
		
		$opt_val = get_option($opt_name);

		$opt_val = array(
			'highlight-actived'		=> (isset($opt_val['highlight-actived'])) ? $opt_val['highlight-actived'] : 0,
			'highlight-category'	=> (isset($opt_val['highlight-category'])) ? $opt_val['highlight-category'] : 1,
			'active-categories'	=> (isset($opt_val['active-categories'])) ? $opt_val['active-categories'] : 0,
			
			'1-category-id'	=> (isset($opt_val['1-category-id'])) ? $opt_val['1-category-id'] : 1,
			'1-title'		=> (isset($opt_val['1-title'])) ? $opt_val['1-title'] : '',
			'1-image'		=> (isset($opt_val['1-image'])) ? $opt_val['1-image'] : '',
			'1-posts'		=> (isset($opt_val['1-posts'])) ? $opt_val['1-posts'] : 3,
			
			'2-category-id'	=> (isset($opt_val['2-category-id'])) ? $opt_val['2-category-id'] : 1,
			'2-title'		=> (isset($opt_val['2-title'])) ? $opt_val['2-title'] : '',
			'2-image'		=> (isset($opt_val['2-image'])) ? $opt_val['2-image'] : '',
			'2-posts'		=> (isset($opt_val['2-posts'])) ? $opt_val['2-posts'] : 3,
			
			'3-category-id'	=> (isset($opt_val['3-category-id'])) ? $opt_val['3-category-id'] : 1,
			'3-title'		=> (isset($opt_val['3-title'])) ? $opt_val['3-title'] : '',
			'3-image'		=> (isset($opt_val['3-image'])) ? $opt_val['3-image'] : '',
			'3-posts'		=> (isset($opt_val['3-posts'])) ? $opt_val['3-posts'] : 3,
		);
		
		echo '<div class="wrap">';
		echo "<h2>Posts da Home:</h2>";
		
		$uri = explode('theme_home_posts', $_SERVER['REQUEST_URI']);
		$form_url = 'http://'.$_SERVER['SERVER_NAME'].$uri[0].'theme_home_posts';
		unset($uri);
		?>
			
			<form name="layout_personalize" id="layout_personalize" method="post" action="<?php echo $form_url ?>" enctype="multipart/form-data">

				<h3>Geral</h3>
				<div id="">
					<p class="">
						<label>Ativar slideshow do topo:</label>
						<input value="0" type="checkbox" name="highlight-actived" id="highlight-actived" <?php echo ($opt_val['highlight-actived'] == 1) ? ' checked="checked"' : ''; ?> />
					</p>
					<p class="">
						<label>Categoria do slideshow:</label>
						<?php echo self::listCats('highlight-category', $opt_val['highlight-category']); ?> 
					</p>
				</div>
				<hr />
				
				<h3>Blocos da home</h3>
				<div id="">
					<p class="">
						<label>Exibir:</label>
						<select id="active-categories" name="active-categories">
							<option value="0"<?php echo ($opt_val['active-categories'] == 0) ? ' selected="selected"' : ''; ?>>Nenhuma</option>
							<option value="1"<?php echo ($opt_val['active-categories'] == 1) ? ' selected="selected"' : ''; ?>>1</option>
							<option value="2"<?php echo ($opt_val['active-categories'] == 2) ? ' selected="selected"' : ''; ?>>2</option>
							<option value="3"<?php echo ($opt_val['active-categories'] == 3) ? ' selected="selected"' : ''; ?>>3</option>
						</select>
					</p>
					<hr />
					<?php

					for($i = 1; $i <= 3; $i++) :
					?>
						<h4>Blocos <?php echo $i ?></h4>
						<p class="">
							<label>Título:</label>
							<input type="text" id="<?php echo $i ?>-title" name="<?php echo $i ?>-title" value="<?php echo $opt_val[$i.'-title'] ?>" size="100" />
						</p>
						<p class="">
							<label>Imagem:</label>
							<input type="file" id="<?php echo $i ?>-image" name="<?php echo $i ?>-image" /> <img src="<?php echo $opt_val[$i.'-image'] ?>" /> <?php echo self::resetOption($i.'-image', 'theme_home_posts') ?>
						</p>
						<p class="">
							<label>Categoria:</label>
							<?php echo self::listCats($i.'-category-id', $opt_val[$i.'-category-id']); ?> 
						</p>
						<p class="">
							<label>Quantidade de posts:</label>
							<select id="<?php echo $i ?>-posts" name="<?php echo $i ?>-posts">
								<option value="3"<?php echo ($opt_val[$i.'-posts'] == 3) ? ' selected="selected"' : ''; ?>>3</option>
								<option value="6"<?php echo ($opt_val[$i.'-posts'] == 6) ? ' selected="selected"' : ''; ?>>6</option>
								<option value="9"<?php echo ($opt_val[$i.'-posts'] == 9) ? ' selected="selected"' : ''; ?>>9</option>
							</select>
						</p>
						<hr />
					<?php
					endfor;
					?>
				</div>
				
				<p class="submit">
					<input type="submit" name="Submit" value="Salvar" />
				</p>
			</form>
		</div>
		<?php
	 
	}
	
	public static function home_posts()
	{
		// Exibe a o bloco só na capa
		if (preg_match("/\/page\//", $_SERVER['REQUEST_URI']))
			return "";
	
		$opt_val = get_option('tema_casamenteiras_home_posts');
		$opt_val = array(
			'highlight-actived'	=> (isset($opt_val['highlight-actived'])) ? $opt_val['highlight-actived'] : 0,
			'highlight-category'	=> (isset($opt_val['highlight-category'])) ? $opt_val['highlight-category'] : 1,
			
			'active-categories'	=> (isset($opt_val['active-categories'])) ? $opt_val['active-categories'] : 1,
			
			'1-category-id'	=> (isset($opt_val['1-title'])) ? $opt_val['1-category-id'] : 1,
			'1-title'		=> (isset($opt_val['1-title'])) ? $opt_val['1-title'] : get_cat_name($opt_val['1-category-id']),
			'1-image'		=> (isset($opt_val['1-image'])) ? $opt_val['1-image'] : '',
			'1-posts'		=> (isset($opt_val['1-posts'])) ? $opt_val['1-posts'] : 3,
			
			'2-category-id'	=> (isset($opt_val['2-title'])) ? $opt_val['2-category-id'] : 1,
			'2-title'		=> (isset($opt_val['2-title'])) ? $opt_val['2-title'] : get_cat_name($opt_val['2-category-id']),
			'2-image'		=> (isset($opt_val['2-image'])) ? $opt_val['2-image'] : '',
			'2-posts'		=> (isset($opt_val['2-posts'])) ? $opt_val['2-posts'] : 3,
			
			'3-category-id'	=> (isset($opt_val['3-title'])) ? $opt_val['3-category-id'] : 1,
			'3-title'		=> (isset($opt_val['3-title'])) ? $opt_val['3-title'] : get_cat_name($opt_val['3-category-id']),
			'3-image'		=> (isset($opt_val['3-image'])) ? $opt_val['3-image'] : '',
			'3-posts'		=> (isset($opt_val['3-posts'])) ? $opt_val['3-posts'] : 3,
		);
		
		if ($opt_val['active-categories'] == 0)
			return;
		
		for($i = 1; $i <= $opt_val['active-categories']; $i++) :
		?>
		<div class="postContainer">
			<div class="heading">
				<?php if ( ! empty($opt_val[$i.'-image'])) : ?>
				<img src="<?php echo $opt_val[$i.'-image'] ?>" alt="<?php echo $opt_val[$i.'-title'] ?>" />
				<?php endif ?>
				<ul class="dots">
					<li class="active dot dot1"><a href="#"></a></li>
					<li class="dot dot2"><a href="#"></a></li>
					<li class="dot dot3"><a href="#"></a></li>
					<li><a href="<?php echo get_category_link($opt_val[$i.'-category-id']) ?>">Mais</a></li>
				</ul>
			</div>
			<div class="posts">
				<ul class="tab tab1">
				<?php
				$count = 1;
				query_posts('cat='.$opt_val[$i.'-category-id'].'&showposts='.$opt_val[$i.'-posts'].'&orderby=date&order=DESC');
				while (have_posts()) :
					the_post();
				?>
					<?php if($count == 4) { ?></ul><ul class="tab tab2"><?php } ?>
					<?php if($count == 7) { ?></ul><ul class="tab tab3"><?php } ?>
					<li>
						<a href="<?php the_permalink() ?>" class="thumb">
							<img width="170" height="125" alt="<?php smallerexcerpt('18'); ?>" src="<?php echo get_thumbnail(get_the_ID(), "170", "125"); ?>" />
						</a>
						<p class="meta"><?php the_time('j \d\e F \d\e Y'); ?><br />por <?php the_author(); ?></p>
						<h3><a href="<?php the_permalink() ?>" title="<?php the_title() ?>"><?php the_title() ?></a></h3>
						<p><?php smallerexcerpt('18'); ?></p>
					</li>
				<?php
					$count++;
				endwhile;
					wp_reset_query();
				?>
				</ul>
			</div>
		</div>
		<?php
		endfor;
	}
	
	public static function adrotate($tag = 'top') {
		if ( ! function_exists("adrotate_banner") )
			return;
		$response;
		switch ($tag)
		{
			case 'top':
				$response = adrotate_banner("1");
			break;
			case 'slideshow':
				$response = adrotate_banner("2");
			break;
			default:
				$response = adrotate_banner();
			break;
		}
		return ($response !== '') ? $response : FALSE;
	}
	
	static public function gravatar($avatar_defaults) {
        $gravatar = get_bloginfo('template_directory') . '/images/gravatar.png';
        $avatar_defaults[$gravatar] = 'Casamenteiras';
        return $avatar_defaults;
    }

};

$mc = new helper();
add_action('admin_menu', array($mc, 'plugin_menu'));
add_action('wp_head', array($mc, 'head_register'));
add_filter('avatar_defaults', array($mc, 'gravatar'));
unset($mc);

function header_function() {
	if ( ! is_admin())
		wp_enqueue_script('header_function', get_bloginfo('template_directory').'/functions.js');
}
add_action('init', 'header_function');

// Custom fields
$post_meta_boxes = array(
	"thumbnail" => array(
	"name" => "thumbnail",
	"std" => "",
	"title" => "Thumbnail Image",
	"description" => "Add the URL for the thumbnail image, this will get resized throughout the site if using timthumb option"), 

	"bannerImage" => array(
	"name" => "bannerImage",
	"std" => "",
	"title" => "Banner Image",
	"description" => "If different from thumbnail, recommended size is 960 pixels width and 270 pixels height"), 

	"bannerImageCredit" => array(
	"name" => "bannerImageCredit",
	"std" => "Photo Credit: Wikipedia",
	"title" => "Banner Image Credit",
	"description" => "If you want to add a credit to the image (or some other text in its place) add it here."),
);

$page_meta_boxes = array(
	"bannerImage" => array(
	"name" => "bannerImage",
	"std" => "",
	"title" => "Banner Image",
	"description" => "If different from thumbnail, recommended size is 960 pixels width and 270 pixels height"), 

	"pageDescription" => array(
	"name" => "pageDescription",
	"std" => "",
	"title" => "Page Description",
	"description" => "The page description that appears under the page title"), 
);

function post_meta_boxes() {
	global $post, $post_meta_boxes;

	foreach($post_meta_boxes as $meta_box) {
		$meta_box_value = get_post_meta($post->ID, $meta_box['name'].'', true);

		if($meta_box_value == "") {
			$meta_box_value = $meta_box['std'];
		}
		echo'<input type="hidden" name="'.$meta_box['name'].'_noncename" id="'.$meta_box['name'].'_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';
		echo'<p style="margin-bottom:20px;"><label style="width:140px;display:block;float:left;margin-top:3px;" for="'.$meta_box['name'].'">'.$meta_box['title'].'</label>';
		echo'<input style="color:#666;" type="text" name="'.$meta_box['name'].'" value="'.$meta_box_value.'" size="55" /><br />';
		echo'<small style="padding-left:140px;padding-top:3px;">'.$meta_box['description'].'</small></p>';
	}
}

function page_meta_boxes() {
	global $post, $page_meta_boxes;

	foreach($page_meta_boxes as $meta_box) {
		$meta_box_value = get_post_meta($post->ID, $meta_box['name'].'', true);

		if ($meta_box_value == "") {
			$meta_box_value = $meta_box['std'];
		};
		echo'<input type="hidden" name="'.$meta_box['name'].'_noncename" id="'.$meta_box['name'].'_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';
		echo'<p style="margin-bottom:20px;"><label style="width:140px;display:block;float:left;margin-top:3px;" for="'.$meta_box['name'].'">'.$meta_box['title'].'</label>';
		echo'<input style="color:#666;" type="text" name="'.$meta_box['name'].'" value="'.$meta_box_value.'" size="55" /><br />';
		echo'<small style="padding-left:140px;padding-top:3px;">'.$meta_box['description'].'</small></p>';
	}
}

function create_meta_box() {
	global $theme_name;
	if ( function_exists('add_meta_box') ) {
		add_meta_box( 'new-meta-boxes', 'Post Options', 'post_meta_boxes', 'post', 'normal', 'high' );
		add_meta_box( 'new-meta-boxes', 'Page Options', 'page_meta_boxes', 'page', 'normal', 'high' );
	}
}

function save_postdata( $post_id ) {
	global $post, $post_meta_boxes;

	foreach($post_meta_boxes as $meta_box) {
		// Verify
		if ( !wp_verify_nonce( $_POST[$meta_box['name'].'_noncename'], plugin_basename(__FILE__) )) {
			return $post_id;
		}

		if ( 'page' == $_POST['post_type'] ) {
			if ( !current_user_can( 'edit_page', $post_id ))
			return $post_id;
		} else {
			if ( !current_user_can( 'edit_post', $post_id ))
			return $post_id;
		}

		$data = $_POST[$meta_box['name'].''];

		if (get_post_meta($post_id, $meta_box['name'].'') == "") {
			add_post_meta($post_id, $meta_box['name'].'', $data, true);
		} elseif ($data != get_post_meta($post_id, $meta_box['name'].'', true)) {
			update_post_meta($post_id, $meta_box['name'].'', $data);
		} elseif ($data == "") {
			delete_post_meta($post_id, $meta_box['name'].'', get_post_meta($post_id, $meta_box['name'].'', true));
		};
	}
}

function save_pagedata( $post_id ) {
	global $post, $page_meta_boxes;

	foreach($page_meta_boxes as $meta_box) {
		// Verify
		if ( !wp_verify_nonce( $_POST[$meta_box['name'].'_noncename'], plugin_basename(__FILE__) )) {
			return $post_id;
		}

		if ( 'page' == $_POST['post_type'] ) {
			if ( !current_user_can( 'edit_page', $post_id )) {
				return $post_id;
			};
		} elseif ( !current_user_can( 'edit_post', $post_id )) {
			return $post_id;
		};

		$data = $_POST[$meta_box['name'].''];

		if(get_post_meta($post_id, $meta_box['name'].'') == "") {
			add_post_meta($post_id, $meta_box['name'].'', $data, true);
		} elseif ($data != get_post_meta($post_id, $meta_box['name'].'', true)) {
			update_post_meta($post_id, $meta_box['name'].'', $data);
		} elseif($data == "") {
			delete_post_meta($post_id, $meta_box['name'].'', get_post_meta($post_id, $meta_box['name'].'', true));
		};
	}
}
add_action('admin_menu', 'create_meta_box');
add_action('save_post', 'save_postdata');
add_action('save_post', 'save_pagedata');

function get_thumbnail($id, $w, $h) {
	$url = get_post_meta($id, 'thumbnail', true);
	if($w == "") { $w="100"; }
	if($h == "") { $h="100"; }
	if($zc == "") { $zc="1"; }
	$q="50";
	if($url == "") {
		return get_banner_image($id, $w, $h);
	} else {
		return get_bloginfo('template_directory').'/scripts/timthumb.php?src='.$url.'&amp;w='.$w.'&amp;h='.$h.'&amp;zc='.$zc.'&amp;q='.$q.'';
	}
}

function get_banner_image($id, $w, $h) {
	$zc=1;
	$q=50;
	$url = get_post_meta($id, 'bannerImage', true);
	if($url=="") { $url = get_post_meta($id,'thumbnail',true); }
	if($url == "") {
		global $post, $posts;
		$first_img = '';
		ob_start();
		ob_end_clean();
		$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
		$first_img = $matches [1] [0];
		if(empty($first_img)){ 
			$first_img = get_bloginfo('template_directory') . "/images/defaultImage.jpg";
		}	
			$imageURL = get_bloginfo('template_directory').'/scripts/timthumb.php?src='.$first_img.'&amp;w='.$w.'&amp;h='.$h.'&amp;zc='.$zc.'&amp;q='.$q.'';
		return $imageURL;
	} else {
		$imageURL = get_bloginfo('template_directory').'/scripts/timthumb.php?src='.$url.'&amp;w='.$w.'&amp;h='.$h.'&amp;zc='.$zc.'&amp;q='.$q.'';
		return $imageURL;
	}
}


function get_banner_image_noTimThumb($id) {
	$url = get_post_meta($id, 'bannerImage', true);
	if($url=="") { $url = get_post_meta($id,'thumbnail',true); }
	if($url == "") {
		global $post, $posts;
		$first_img = '';
		ob_start();
		ob_end_clean();
		$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
		$first_img = $matches [1] [0];
		if(empty($first_img)){ 
			$first_img = get_bloginfo('template_directory') . "/images/defaultImage.jpg";
		}
		return $first_img;
	} else {
		return $url;
	}
}

function smallerexcerpt($num) {
	$limit = $num+1;
	$excerpt = explode(' ', get_the_excerpt(), $limit);
	array_pop($excerpt);
	$excerpt = implode(" ",$excerpt)."...";
	echo $excerpt;
}